﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.Models;
using PNProject.EF;
namespace PNProject.Controllers
{
    
    public class AdminController : Controller
    {
        
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            var roles = Roles.GetRolesForUser();
            if (roles.Length == 0)
            {
                return RedirectToAction("Logon");
            }
            else
            {
                bool flag = false;
                for (int i = 0; i < roles.Length; i++)
                {
                    if (roles[i] == "SuperAdmin" || roles[i] == "Admin" || roles[i] == "Censor")
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                    return RedirectToAction("Info");
                else
                    return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult Logon()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Logon(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                bool flag = AdminService.CheckUser(model.UserName);
                if (flag && Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    var roles = Roles.GetRolesForUser();
                    if (roles.Length == 0)
                    {
                        return RedirectToAction("Logon");
                    }
                    else
                    {
                        bool flag2 = false;
                        for (int i = 0; i < roles.Length; i++)
                        {
                            if (roles[i] == "SuperAdmin" || roles[i] == "Admin" || roles[i] == "Censor")
                            {
                                flag2 = true;
                                break;
                            }
                        }
                        if (flag2)
                            return RedirectToAction("Info");
                        else
                            return RedirectToAction("Profile", "Member");
                    }
                }
                else
                {
                    return RedirectToAction("Index", "Admin");
                }
            }
            return RedirectToAction("Index", "Admin");
        }

        [Authorize(Roles = "SuperAdmin, Censor, Admin")]
        public ActionResult Info()
        {
            return View();
        }

        [Authorize(Roles = "SuperAdmin, Censor, Admin")]
        public ActionResult UserDetail()
        {
            PNUser user = new PNUser();
            var username = Membership.GetUser().UserName;
            var xuser = StaticData.xuserRepo.GetOne(a => a.Username == username);
            user.Name = xuser.Name;
            user.Email = xuser.Email;
            user.Username = username;
            user.Avatar = xuser.Avatar;
            return View(user);
        }

        [Authorize]
        public ActionResult Error()
        {
            return View();
        }

    }
}
